package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exception.LyException;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.service.SpecParamService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpecParamServiceImpl extends ServiceImpl<SpecParamMapper, SpecParam>implements SpecParamService {


    //根据条件查询规格参数
    @Override
    public List<SpecParamDTO> listSpecParam(Long cid, Long gid, Boolean searching) {
        List<SpecParam> specParams = this.query().eq(cid != null, "category_id", cid)
                .eq(gid != null, "group_id", gid)
                .eq(searching != null, "searching", searching)
                .list();
        return SpecParamDTO.convertEntityList(specParams);
    }

    //添加规格参数
    @Override
    public void addSpecParam(SpecParamDTO specParamDTO) {
        SpecParam specParam = specParamDTO.toEntity(SpecParam.class);
        boolean save = this.save(specParam);
        if(!save){
            throw new LyException(500,"新增规格参数失败");

        }
    }
    //删除规格参数
    @Override
    public void deleteSpecParam(SpecParamDTO specParamDTO) {
        SpecParam specParam = specParamDTO.toEntity(SpecParam.class);
        boolean b = this.removeById(specParam.getId());
        if (!b){
            throw new LyException(500,"删除规格参数失败");
        }

    }

    //新增规格参数
    @Override
    public void updateSpecGroup(SpecParamDTO specParamDTO) {
        SpecParam specParam = specParamDTO.toEntity(SpecParam.class);
        boolean b = this.updateById(specParam);
        if (!b){
            throw new LyException(500,"修改规格参数失败");
        }
    }
}
